package com.cap.bts.crmdp.dealer.remind.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cap.bts.crmdp.dealer.remind.dto.RemindSettingsDto;
import com.cap.bts.crmdp.dealer.remind.service.IRemindService;
import com.cap.bts.framework.codemapping.domain.CodeInfo;
import com.cap.bts.framework.codemapping.service.ICodeMapping;
import com.cap.bts.framework.common.controller.BaseController;
import com.cap.bts.framework.common.exception.AppException;

/**
 * 
 */
@Controller
@RequestMapping(value = "/crmdp/dealer/remind")
public class RemindSettingController extends BaseController {
    
    @Autowired
    IRemindService remindService;
    
    @Autowired
    ICodeMapping codeMapping;
    
    @RequestMapping(value = "/toRemindSetting")
    public String toRemindSettings(RemindSettingsDto dto, Model model, HttpServletRequest request)
        throws AppException {
        
        CodeInfo  remindDefaultQuantityCodeInfo=codeMapping.getCodeInfoByCode("RemindDefaultQuantity");
        CodeInfo  remindDaysCodeInfo=codeMapping.getCodeInfoByCode("RemindDays");
        CodeInfo  afterRemindDaysCodeInfo=codeMapping.getCodeInfoByCode("AfterRemindDays");
        String remindDefaultQuantity=remindDefaultQuantityCodeInfo.getValue();
        String remindDays=remindDaysCodeInfo.getValue();
        String afterRemindDays=afterRemindDaysCodeInfo.getValue();
        Long remindDefaultQuantityId=remindDefaultQuantityCodeInfo.getId();
        Long remindDaysId=remindDaysCodeInfo.getId();
        Long afterRemindDaysId=afterRemindDaysCodeInfo.getId();
        List<CodeInfo> msgPackageList=codeMapping.getCodeInfoByType("SMS");
        List<CodeInfo> servicTypeList=codeMapping.getCodeInfoByTypeIgnoreDeleteFlag("ServiceRemind");
        model.addAttribute("remindDefaultQuantity", remindDefaultQuantity);
        model.addAttribute("remindDays", remindDays);
        model.addAttribute("afterRemindDays", afterRemindDays);
        model.addAttribute("remindDefaultQuantityId", remindDefaultQuantityId);
        model.addAttribute("remindDaysId", remindDaysId);
        model.addAttribute("afterRemindDaysId", afterRemindDaysId);
        model.addAttribute("msgPackageList", msgPackageList);
        model.addAttribute("servicTypeList", servicTypeList);
        return "remind/remindSetting";
        
    }
    
    
    @RequestMapping(value = "/saveRemindSetting")
    public String saveRemindSetting(RemindSettingsDto dto, Model model, HttpServletRequest request)
        throws AppException {
        remindService.saveRemindSetting(dto);
        return toRemindSettings(dto,  model, request);
    }
    
    
    @RequestMapping(value = "/deleteMsgPackage")
    @ResponseBody
    public Map<String,Object>  deleteMsgPackage(Long id, Model model, HttpServletRequest request)
        throws AppException {
        Map<String,Object> rtnMap=new HashMap<String, Object>();
        rtnMap.put("result", true);
        codeMapping.physicalDelete(id);
        return rtnMap;
    }
    

    @RequestMapping(value = "/modifyMsgPackage")
    public String modifyMsgPackage(Long id, Model model, HttpServletRequest request)
        throws AppException {
        CodeInfo codeInfo=codeMapping.findById(id);
        model.addAttribute("codeInfo", codeInfo);
        model.addAttribute("isEdit", true);
        return "remind/msgPackageEdit";
    }
    
    @RequestMapping(value = "/addMsgPackage")
    public String addMsgPackage(Long id, Model model, HttpServletRequest request)
        throws AppException {
        model.addAttribute("codeInfo", null);
        model.addAttribute("isEdit", false);
        return "remind/msgPackageEdit";
    }
    
    @RequestMapping(value = "/saveMsgPackage")
    @ResponseBody
    public Map<String,Object>  saveMsgPackage(CodeInfo codeInfo, Model model, HttpServletRequest request)
        throws AppException {
        Map<String,Object> rtnMap=new HashMap<String, Object>();
        rtnMap.put("result", true);
        if(codeInfo.getId()!=null){
            codeMapping.updateCodeInfo(codeInfo);
        }else{
            CodeInfo codeInfosave=new CodeInfo();
            codeInfosave.setValue(codeInfo.getValue());
            codeInfosave.setCode(codeInfo.getCode());
            codeInfosave.setType("SMS");
            codeMapping.addCodeInfo(codeInfosave);
        }
        model.addAttribute("codeInfo", codeInfo);
        model.addAttribute("isEdit", true);
        return rtnMap;
    }
}
